//
//  SettingView.swift
//  Chatup
//
//  Created by op on 2023/10/17.
//

import SwiftUI

struct ShareView: UIViewControllerRepresentable {
    @Environment(\.presentationMode) var presentation
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<ShareView>) -> UIActivityViewController {
        let textToShare = "I find an excellent chatting app as my AI assistant. Let's have a chat now !"
        let appStoreLink = URL(string: "https://itunes.apple.com/app/6470002427")!
        let objectsToShare: [Any] = [textToShare, appStoreLink]
        
        let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        return activityViewController
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
        
    }
}


struct SettingView: View {
  @StateObject var viewModel = SettingViewModel()
    @Environment (\.presentationMode) var prensentModel
  var body: some View {
      VStack {
          HStack {
              Button(action: {prensentModel.wrappedValue.dismiss()}, label: {
                  Image("icon_back")
              })
              Spacer()
              Text(String.i18nText("settings"))
                  .font(.system(size: 20,weight: .bold))
                  .foregroundColor(.white)
              Spacer()
              Button(action: {}, label: {
                  Image("icon_close")
              })
              .opacity(0)
          }
          .padding(.horizontal)
          .padding(.vertical,8)
          
          ScrollView {
              VStack {
                  Spacer()
                      .frame(height: 26)
                  ForEach(viewModel.items.indices, id: \.self) { index in
                      if index != 2{
                          Button(action: {
                              viewModel.clickItem(index:index)
                          }, label: {
                              VStack {
                                  HStack {
                                      Spacer()
                                          .frame(width: 12)
                                      Image(viewModel.items[index].icon)
                                          .frame(width: 18, height: 18)
                                      Spacer()
                                          .frame(width: 12)
                                      Text(viewModel.items[index].name)
                                          .font(Font.custom("SF Pro Display", size: 16))
                                          .foregroundColor(.white)
                                      Spacer()
                                  }
                                  .padding()

//                                  Divider()
//                                  .background(Color(red: 0.09, green: 0.09, blue: 0.13))
                              }
//                              .contentShape(Rectangle())
//                              .onTapGesture {
//                                  viewModel.clickItem(index:index)
//                              }
                          })
                      } else {
                          Button(action: {
                              viewModel.clickItem(index:index)
                          }, label: {
                              VStack {
                                  HStack {
                                      Spacer()
                                          .frame(width: 12)
                                      Image(viewModel.items[index].icon)
                                          .frame(width: 18, height: 18)
                                      Spacer()
                                          .frame(width: 12)
                                      Text(viewModel.items[index].name)
                                          .font(Font.custom("SF Pro Display", size: 16))
                                          .foregroundColor(.white)
                                      Spacer()
                                      Image("icon_setting_vip_right")
                                          .resizable()
                                          .frame(width: 77,height: 24)
                                  }
                                  .padding()

//                                  Divider()
//                                  .background(Color(red: 0.09, green: 0.09, blue: 0.13))
                              }
//                              .contentShape(Rectangle())
//                              .onTapGesture {
//                                  viewModel.clickItem(index:index)
//                              }
                          })
                      }
                }
                  #if DEBUG
                  VStack(alignment:.leading) {
                      HStack {
                          Text("DEBUG")
                          Spacer()
                      }
                      HStack {
                          Button("清除次数限制") {
                              UserDefaults.standard.setValue(3, forKey: "leftCount")
                              UserDefaults.standard.synchronize()
                          }
                          Spacer()
                      }
                      HStack {
                          let isVip = UserDefaults.standard.bool(forKey: "kWhetherVip")
                          Text("是否是vip: \(String(isVip))")
                          Spacer()
                      }
                      HStack {
                          Toggle("切换vip", isOn: $viewModel.switchVIP)
                          Spacer()
                      }
                      HStack {
                          Button("恢复新手引导") {
                              UserDefaults.standard.setValue(false, forKey: "kHasSeenGuide")
                              UserDefaults.standard.synchronize()
                          }
                          Spacer()
                      }
                  }
                  .padding()
                  #endif
              }
          }
      }
      .fullScreenCover(isPresented: $viewModel.showVip, content: {
          VipView(viewModel: .init(from: "settings"))
      })
      .sheet(isPresented: $viewModel.isShowingShareView) {
          ShareView()
      }
      .onAppear{
          viewModel.switchVIP = UserDefaults.standard.bool(forKey: "kWhetherVip")
      }

  }
}

#Preview {
    SettingView()
}
